//
//  NSString+Extend.m
//  iOSNoteList
//
//  Created by LuPengDa on 15/6/11.
//  Copyright (c) 2015年 xj. All rights reserved.
//

#import "NSString+Extend.h"

#import <CommonCrypto/CommonDigest.h>

#define CURR_LANG ([[NSLocale preferredLanguages] objectAtIndex:0])

@implementation NSString (Extend)
#pragma mark - base64
- (NSString *)encodeBase64
{
    return [[self dataUsingEncoding:NSUTF8StringEncoding] base64EncodedStringWithOptions:0];
}
- (NSString *)decodeBase64
{
    NSData* decodeData = [[NSData alloc] initWithBase64EncodedString:self options:0];
    NSString* decodeStr = [[NSString alloc] initWithData:decodeData encoding:NSASCIIStringEncoding];
    return decodeStr;
}
#pragma mark - MD5
- (NSString *)md5
{
    NSData *data = [self dataUsingEncoding:NSUTF8StringEncoding];
    
    const char *input = [data bytes];
    unsigned char md5Buffer[CC_MD5_DIGEST_LENGTH];
    CC_MD5(input, (CC_LONG)data.length, md5Buffer);
    
    NSMutableString *md5 = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
    for (int i = 0; i < CC_MD5_DIGEST_LENGTH; i++) {
        [md5 appendFormat:@"%02X", md5Buffer[i]];
    }
    
    return [md5 lowercaseString];
}

+(BOOL) isValidateMobile:(NSString *)mobile
{
    if (mobile.length != 11) {
        return NO;
    }
    NSString *checkedNumString = [mobile stringByTrimmingCharactersInSet:[NSCharacterSet decimalDigitCharacterSet]];
    if(checkedNumString.length > 0) {
        return NO;
    }
    return YES;
}

#pragma mark - Localizable
+ (instancetype)localizedStringWithKey:(NSString *)key comment:(NSString *)comment
{
    NSString *localizedStr = NSLocalizedString(key, comment);
    if (![CURR_LANG isEqual:@"en"] && ![CURR_LANG isEqual:@"zh-Hans"]) {
        NSString * path = [[NSBundle mainBundle] pathForResource:@"zh-Hans" ofType:@"lproj"];
        NSBundle * languageBundle = [NSBundle bundleWithPath:path];
        localizedStr = [languageBundle localizedStringForKey:key value:@"" table:nil];
    }
    return localizedStr;
}

#pragma mark - size
- (CGSize)sizeWithMyFont:(UIFont *)font
{
    NSDictionary *textAttributes = @{NSFontAttributeName : font};
    CGSize size=[self sizeWithAttributes:textAttributes];
    return size;
}

- (CGSize)sizeWithMyFont:(UIFont *)font constrainedToSize:(CGSize)size
{
    NSDictionary *textAttributes = @{NSFontAttributeName : font};
    CGRect rect = [self boundingRectWithSize:size
                                     options:NSStringDrawingUsesLineFragmentOrigin
                                  attributes:textAttributes
                                     context:nil];
    return rect.size;
}


#pragma mark - containsString:
- (BOOL)TQContainsString:(NSString *)str
{
    NSRange range = [self rangeOfString:str];
    return (range.location != NSNotFound);
}

@end

@implementation NSObject (TQExtend)

- (BOOL)isEmpty
{
    if (!self || [self isEqual:[NSNull null]]) {
        return YES;
    }
    
    if ([self isKindOfClass:[NSString class]] && [(NSString *)self isEqualToString:@""]) {
        return YES;
    } else {
        return NO;
    }
}

BOOL isEmpty(id obj)
{
    if (!obj || [obj isEqual:[NSNull null]]) {
        return YES;
    }
    
    if ([obj isKindOfClass:[NSString class]] && [(NSString *)obj isEqualToString:@""]) {
        return YES;
    } else {
        return NO;
    }
}

NSString *toString(id obj)
{
    if (isEmpty(obj)) {
        return @"";
    }
    return obj;
}

#pragma mark-获取中间字符串-（根据开始和结束字符串截取）
-(NSString*)GetStringRangefirst:(NSString*)firststring laststring:(NSString*)laststring TargetString:(NSString*)string
{
    
    NSRange firstrange =[string rangeOfString:firststring];
    NSRange lastrange=[string rangeOfString:laststring];
    NSInteger index=firstrange.location+firstrange.length;
    NSString* newstring=[string substringWithRange:NSMakeRange(index,lastrange.location-index)];
    NSString* strs=[newstring stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    
    return strs;
}
-(BOOL)hasUpdateonLineVersion:(NSString *)onlineVersion currentVersion:(NSString *)currentVersion {
    NSArray *arr1 = [onlineVersion componentsSeparatedByString:@"."];
    NSArray *arr2 = [currentVersion componentsSeparatedByString:@"."];
    if (arr1.count >0 && arr2.count >0) {
        NSString *str11 = arr1[0];
        NSString *str21 = arr2[0];
        if ([str11 intValue] > [str21 intValue]) {
            return YES;
        }
        if ([str11 intValue] < [str21 intValue]) {
            return NO;
        }
    }
    if (arr1.count >1 && arr2.count >1) {
        NSString *str11 = arr1[1];
        NSString *str21 = arr2[1];
        if ([str11 intValue] > [str21 intValue]) {
            return YES;
        }
        if ([str11 intValue] < [str21 intValue]) {
            return NO;
        }
    }
    if (arr1.count >2 && arr2.count >2) {
        NSString *str11 = arr1[2];
        NSString *str21 = arr2[2];
        if ([str11 intValue] > [str21 intValue]) {
            return YES;
        }
    }
    return NO;
}

-(BOOL)isBigThanOnlineVersion:(NSString *)currentVersion onlineVersion:(NSString *)onlineVersion{
    NSArray *arr1 = [currentVersion componentsSeparatedByString:@"."];
    NSArray *arr2 = [onlineVersion componentsSeparatedByString:@"."];
    if (arr1.count >0 && arr2.count >0) {
        NSString *str11 = arr1[0];
        NSString *str21 = arr2[0];
        if ([str11 intValue] > [str21 intValue]) {
            return YES;
        }
        if ([str11 intValue] < [str21 intValue]) {
            return NO;
        }
    }
    if (arr1.count >1 && arr2.count >1) {
        NSString *str11 = arr1[1];
        NSString *str21 = arr2[1];
        if ([str11 intValue] > [str21 intValue]) {
            return YES;
        }
        if ([str11 intValue] < [str21 intValue]) {
            return NO;
        }
    }
    if (arr1.count >2 && arr2.count >2) {
        NSString *str11 = arr1[2];
        NSString *str21 = arr2[2];
        if ([str11 intValue] > [str21 intValue]) {
            return YES;
        }
    }
    return NO;
}
@end
